import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/home/Login.vue')
  },
  {
    path: '/404',
    component: () => import(/* webpackChunkName: "about" */ '../views/error/404.vue')
  },
  {
    path: '/401',
    component: () => import(/* webpackChunkName: "about" */ '../views/error/401.vue')
  },
  {
    path: '/home',
    name: '主页',
    redirect: '/welcome',
    component: () => import(/* webpackChunkName: "about" */ '../views/home/Home.vue'),
    children: [
      {
        path: '/sys',
        name: '系统管理',
        component: () => import(/* webpackChunkName: "about" */ '../views/home/Layout'),
        children: [
          {
            path: '/sys/users',
            name: '用户管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/user/Users.vue'),
          },
          {
            path: '/roles',
            name: '角色管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/role/Roles'),
          },
          {
            path: '/attachments',
            name: '附件管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/files/Files.vue'),
          },
          {
            path: '/menus',
            name: '菜单管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/menu/Menus'),
          },
          {
            path: '/departments',
            name: '部门管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/department/Departments'),
          },
          {
            path: '/notices',
            name: '公告管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/system/notice/Notices'),
          },
          {
            path: '/log',
            name: '日志管理',
            component: () => import(/* webpackChunkName: "about" */ '../views/home/Layout'),
            children: [
              {
                path: '/logs',
                name: '操作日志',
                component: () => import(/* webpackChunkName: "about" */ '../views/system/log/Logs'),
              },
              {
                path: '/loginLog',
                name: '登陆日志',
                component: () => import(/* webpackChunkName: "about" */ '../views/system/log/LoginLog'),
              },
            ]
          }
        ]
      },
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import(/* webpackChunkName: "about" */ '../views/home/Welcome'),
      },

      {
        path: '/products',
        name: 'Products',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/product/Products'),
      },
      {
        path: '/productCategorys',
        name: 'ProductCategorys',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/product/ProductCategory'),
      },
      {
        path: '/inStocks',
        name: 'InStocks',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/product/InStocks'),
      },
      {
        path: '/outStocks',
        name: 'OutStocks',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/product/OutStocks'),
      },
      {
        path: '/stocks',
        name: 'Stocks',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/product/Stocks'),
      },
      {
        path: '/suppliers',
        name: 'Suppliers',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/flow/Suppliers'),
      },
      {
        path: '/consumers',
        name: 'Consumers',
        component: () => import(/* webpackChunkName: "about" */ '../views/biz/flow/Consumers'),
      },
      {
        path: '/map',
        name: 'Map',
        component: () => import(/* webpackChunkName: "about" */ '../views/system/map/Map.vue'),
      },
      {
        path: '/swagger',
        component: () => import(/* webpackChunkName: "about" */ '../views/others/Swagger.vue')
      },
      {
        path: '/icons',
        component: () => import(/* webpackChunkName: "about" */ '../views/others/Icons.vue')
      },
      {
        path: '/userProfile',
        name: 'UserProfile',
        component: () => import(/* webpackChunkName: "about" */ '../views/system/user/UserProfile.vue'),
      },
      {
        path: '/s3',
        name: 'S3',
        component: () => import(/* webpackChunkName: "about" */ '../views/work/S3.vue'),
      },
      {
        path: '/s4',
        name: 'S4',
        component: () => import(/* webpackChunkName: "about" */ '../views/work/S4.vue'),
      },
      {
        path: '/s4_1',
        name: 'S4_1',
        component: () => import(/* webpackChunkName: "about" */ '../views/work/S4_1.vue'),
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

import store from '../store'//引入store

//路由导航守卫
router.beforeEach((to, from, next) => {

  const allUrl = window.sessionStorage.getItem('urls')
  const token = window.sessionStorage.getItem('tokenStr')
  if (to.path === '/login') {
    if (!token) {
      return next()
    } else {
      return next({ path: '/home' })
    }
  }

  if (to.path === '/401' || to.path === '/404' || to.path === '/s4_1' || to.path === '/userProfile' || to.path === '/welcome') {
    return next()
  }

  if (!token) {
    return next('/login')
  } else {
    if (allUrl.indexOf(to.path) > -1) {
      //判断是否有访问该路径的权限
      const urls = store.state.userInfo.url
      if (urls.indexOf(to.path) > -1) {
        //则包含该元素
        return next()
      } else {
        return next('/401')
      }
    } else {
      return next('/404')
    }
  }
})
export default router

